package com.tanhua.server.controller;

import com.fasterxml.jackson.databind.ser.impl.UnknownSerializer;
import com.tanhua.domain.db.User;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.server.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ResponseBody 自动把方法返回的对象转换为json并写入到response输出流
 * @RequestBody  把请求的json字符串转换为javabean对象
 */
@RestController  // @RestController = @Controller + @ResponseBody
@RequestMapping("user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 接口名称：登录第一步---手机号登录
     * 接口路径：POST/user/login
     * 需求描述：发送验证码
     */
    @PostMapping("login")
    public ResponseEntity<Object> login(@RequestBody Map<String,String> map){
        // 获取请求参数 - 手机号码
        String phone = map.get("phone");
        // 调用service，实现发送短信的业务
        return userService.login(phone);
    }

    /**
     * 接口名称：登录第二步---验证码校验
     * 接口路径：POST/user/loginVerification
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String,String> map) throws ParseException {
        // 获取请求数据
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        // 调用service，完整验证码校验
        return userService.loginVerification(phone,verificationCode);
    }
}
